#!/bin/bash

# 获取最新版
# Url=$(curl -Ls https://github.com/prometheus/mysqld_exporter/releases |awk -F'[ "]+' '/download.*mysqld_exporter-.*.linux-amd64.tar.gz/{print "https://github.com"$4 }' |head -n 1)
# curl -Lso /tmp/mysqld_exporter.tar.gz $Url

# 下载软件
VERSION=0.12.1
curl -SLO http://dl.hc-yun.com/soft/monitor/mysqld_exporter-${VERSION}.linux-amd64.tar.gz

# 解压配置
INSTALL_DIR=/usr/local/mysqld_exporter
[ -d "$INSTALL_DIR" ] && mv $INSTALL_DIR ${INSTALL_DIR}-$(date +%s)
tar xf mysqld_exporter-${VERSION}.linux-amd64.tar.gz
mv mysqld_exporte-${VERSION}.linux-amd64 $INSTALL_DIR

# 创建服务
cat > /usr/lib/systemd/system/mysqld_exporter.service <<'EOF'
[Unit]
Description=mysqld_exporter server daemon

[Service]
Restart=on-failure
ExecStart=${INSTALL_DIR}/mysqld_exporter --config.my-cnf=${INSTALL_DIR}/my.cnf
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

# 启动服务跟随系统启动
systemctl daemon-reload
systemctl enable mysqld_exporter
systemctl restart mysqld_exporter
systemctl status mysqld_exporter

echo "mysqld_exporter dashboard: https://grafana.com/grafana/dashboards/7362"
echo
echo "metrics:  http://$(hostname -i):9104/metrics"